package com.lanyan.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lanyan.common.utils.DateUtils;
import com.lanyan.system.domain.Account;
import com.lanyan.system.domain.Pay;
import com.lanyan.system.mapper.AccountMapper;
import com.lanyan.system.mapper.PayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.mapper.CouponMapper;
import com.lanyan.system.domain.Coupon;
import com.lanyan.system.domain.vo.CouponVo;
import com.lanyan.system.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 优惠券Service业务层处理
 *
 * @author lanyan
 * @date 2024-11-26
 */
@Service
public class CouponServiceImpl  extends ServiceImpl<CouponMapper,Coupon> implements ICouponService

{
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private PayMapper payMapper;

    /**
     * 查询优惠券
     *
     * @param couponId 优惠券主键
     * @return 优惠券
     */
    @Override
    public Coupon selectCouponByCouponId(Long couponId)
    {
        return this.getById(couponId);
    }

    /**
     * 查询优惠券Vo列表
     *
     * @param coupon 优惠券
     * @return 优惠券
     */
    @Override
    public List<CouponVo> selectCouponVoList(Coupon coupon)
    {
        return selectCouponList(coupon)
                .stream()
                .map(x -> (CouponVo)x)
                .collect(Collectors.toList());
    }
    /**
     * 查询优惠券列表
     *
     * @param coupon 优惠券
     * @return 优惠券
     */
    @Override
    public List<Coupon> selectCouponList(Coupon coupon)
    {
        LambdaQueryChainWrapper<Coupon> wrapper = this.lambdaQuery().eq(Coupon::getDelFlag,"0");
        if (StringUtils.isNotEmpty(coupon.getName())) {
            wrapper.like(Coupon::getName, coupon.getName());
        }
        if (coupon.getNeed() != null) {
            wrapper.eq(Coupon::getNeed, coupon.getNeed());
        }
        if (coupon.getDerate() != null) {
            wrapper.eq(Coupon::getDerate, coupon.getDerate());
        }
        if (StringUtils.isNotEmpty(coupon.getTimer())) {
            wrapper.eq(Coupon::getTimer, coupon.getTimer());
        }
        return wrapper.list();
    }

        /**
         * 新增优惠券
         *
         * @param coupon 优惠券
         * @return 结果
         */
    @Override
        public boolean insertCoupon(Coupon coupon)
        {
                    coupon.setCreateTime(DateUtils.getNowDate());
                return this.save(coupon);
        }

        /**
         * 修改优惠券
         *
         * @param coupon 优惠券
         * @return 结果
         */
    @Override
        public boolean updateCoupon(Coupon coupon)
        {
            if (coupon.getConsumeAmount() != null) {
                coupon.setConsumeAmount(coupon.getConsumeAmount());
            }else {
                coupon.setConsumeAmount(null);
            }
            coupon.setUpdateTime(DateUtils.getNowDate());
            return this.updateById(coupon);
        }

        /**
         * 批量删除优惠券
         *
         * @param couponIds 需要删除的优惠券主键
         * @return 结果
         */
    @Override
        public boolean deleteCouponByCouponIds(Long[] couponIds)
        {
            return this.lambdaUpdate().set(Coupon::getDelFlag,"1").in(Coupon::getCouponId,couponIds).update();
        }

        /**
         * 删除优惠券信息
         *
         * @param couponId 优惠券主键
         * @return 结果
         */
    @Override
        public boolean deleteCouponByCouponId(Long couponId)
        {
            return this.lambdaUpdate().set(Coupon::getDelFlag,"1").eq(Coupon::getCouponId,couponId).update();
        }

    @Override
    public List<CouponVo> unget(Long accountId) {
        Account account = accountMapper.selectById(accountId);

        //获取用户前一天的消费金额
        //获取前一天的开始时间和结束时间
        Date yesterday = DateUtils.addDays(new Date(), -1);
        //转换成字符串
        String startTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, yesterday);
        String endTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, yesterday);

        LambdaQueryWrapper<Pay> eq = new LambdaQueryWrapper<Pay>().eq(Pay::getAccountId, accountId)
                .between(Pay::getCreateTime,startTime+" 00:00:00",endTime+"23:59:59");
        List<Pay> payList = payMapper.selectList(eq);
        //统计订单金额
        BigDecimal totalMoney = payList.stream().map(Pay::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        //转换城int
        int i = totalMoney.intValue();

        Date createTime = account.getCreateTime();
        Date now = new Date();
        List<CouponVo> unget = couponMapper.unget(accountId, 5);
        //判断用户的创建时间跟当前时间对比是否在一天之内
        if (now.getTime() - createTime.getTime() > 24 * 60 * 60 * 1000) {
            List<CouponVo> unget2 = couponMapper.unget(accountId, 4);
            // 过滤获取consumeAmount大于i的数据
            for (CouponVo couponVo : unget2) {
                Integer consumeAmount = couponVo.getConsumeAmount();
                if (i>consumeAmount){
                    unget.add(couponVo);
                }
            }
            List<CouponVo> unget1 = couponMapper.unget(accountId, 1);
            unget.addAll(unget1);
            return unget;
        }
        //判断用户的创建时间跟当前时间对比是否在满一天不满两天
        if (now.getTime() - createTime.getTime() > 24 * 60 * 60 * 1000 && now.getTime() - createTime.getTime() < 2 * 24 * 60 * 60 * 1000) {
            List<CouponVo> unget2 = couponMapper.unget(accountId, 4);
            // 过滤获取consumeAmount大于i的数据
            for (CouponVo couponVo : unget2) {
                Integer consumeAmount = couponVo.getConsumeAmount();
                if (i>consumeAmount){
                    unget.add(couponVo);
                }
            }
            List<CouponVo> unget1 = couponMapper.unget(accountId, 2);
            unget.addAll(unget1);
            return unget;
        }
        //判断用户的创建时间跟当前时间对比是否在满两天不满三天
        if (now.getTime() - createTime.getTime() > 2 * 24 * 60 * 60 * 1000 && now.getTime() - createTime.getTime() < 3 * 24 * 60 * 60 * 1000) {
            List<CouponVo> unget2 = couponMapper.unget(accountId, 4);
            // 过滤获取consumeAmount大于i的数据
            for (CouponVo couponVo : unget2) {
                Integer consumeAmount = couponVo.getConsumeAmount();
                if (i>consumeAmount){
                    unget.add(couponVo);
                }
            }
            List<CouponVo> unget1 = couponMapper.unget(accountId, 3);
            unget.addAll(unget1);
            return unget;
        }

        return couponMapper.unget(accountId,5);
    }
}
